package com.winit.openapi.util;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.exceptions.JedisConnectionException;
import redis.clients.util.Pool;

import com.winit.common.redis.RedisPoolManager;

public class RedisUtil {

    private static Pool<Jedis> jedisPool = RedisPoolManager.getJedisPool();

    /**
     * 压栈
     * 
     * @param key
     * @param value
     * @return
     */
    public static Long push(String key, String value) {
        Jedis jedis = null;
        boolean borrowOrOprSuccess = true;
        Long l = null;
        try {
            jedis = jedisPool.getResource();
            l = jedis.lpush(key, value);
        } catch (JedisConnectionException e) {
            borrowOrOprSuccess = false;
            if (jedis != null) jedisPool.returnBrokenResource(jedis);
        } finally {
            if (borrowOrOprSuccess) jedisPool.returnResource(jedis);
        }
        return l;
    }

    /**
     * 支持设置失效时间
     * 
     * @param key
     * @param value
     * @param expireTime(seconds)
     * @return
     */
    public static Long put(String key, String value, int expireTime) {
        Jedis jedis = null;
        boolean borrowOrOprSuccess = true;
        Long l = null;
        try {
            jedis = jedisPool.getResource();
            jedis.setex(key, expireTime, value);
        } catch (JedisConnectionException e) {
            borrowOrOprSuccess = false;
            if (jedis != null) jedisPool.returnBrokenResource(jedis);
        } finally {
            if (borrowOrOprSuccess) jedisPool.returnResource(jedis);
        }
        return l;
    }

    /**
     * 当键不存在时就设置，成功返回1 如果键已经存在，不会覆盖，设置失败，返回0
     * 
     * @param key
     * @param value
     * @return
     */
    public static Long putIfNotExists(String key, String value) {
        Jedis jedis = null;
        boolean borrowOrOprSuccess = true;
        Long l = 0L;
        try {
            jedis = jedisPool.getResource();
            l = jedis.setnx(key, value);
        } catch (JedisConnectionException e) {
            borrowOrOprSuccess = false;
            if (jedis != null) jedisPool.returnBrokenResource(jedis);
        } finally {
            if (borrowOrOprSuccess) jedisPool.returnResource(jedis);
        }
        return l;
    }

    /**
     * 设置键的超时时间，单位：秒
     * 
     * @param key
     * @param timeSeconds
     * @return
     */
    public static Long setExpireTime(String key, int timeSeconds) {
        Jedis jedis = null;
        boolean borrowOrOprSuccess = true;
        Long l = null;
        try {
            jedis = jedisPool.getResource();
            l = jedis.expire(key, timeSeconds);
        } catch (JedisConnectionException e) {
            borrowOrOprSuccess = false;
            if (jedis != null) jedisPool.returnBrokenResource(jedis);
        } finally {
            if (borrowOrOprSuccess) jedisPool.returnResource(jedis);
        }
        return l;
    }

    /**
     * 设置缓存值
     * 
     * @param key
     * @param value
     * @param expireTime(seconds)
     * @return
     */
    public static Long put(String key, String value) {
        Jedis jedis = null;
        boolean borrowOrOprSuccess = true;
        Long l = null;
        try {
            jedis = jedisPool.getResource();
            jedis.set(key, value);
        } catch (JedisConnectionException e) {
            borrowOrOprSuccess = false;
            if (jedis != null) jedisPool.returnBrokenResource(jedis);
        } finally {
            if (borrowOrOprSuccess) jedisPool.returnResource(jedis);
        }
        return l;
    }

    /**
     * 出栈
     * 
     * @param key
     * @return
     */
    public static String get(String key) {
        Jedis jedis = null;
        boolean borrowOrOprSuccess = true;
        String s = null;
        try {
            jedis = jedisPool.getResource();
            s = jedis.get(key);
        } catch (JedisConnectionException e) {
            borrowOrOprSuccess = false;
            if (jedis != null) jedisPool.returnBrokenResource(jedis);
        } finally {
            if (borrowOrOprSuccess) jedisPool.returnResource(jedis);
        }
        return s;
    }

    /**
     * 获取键剩余生命周期
     * 
     * @param key
     * @return
     */
    public static Long getExpireTime(String key) {
        Jedis jedis = null;
        boolean borrowOrOprSuccess = true;
        Long l = null;
        try {
            jedis = jedisPool.getResource();
            l = jedis.ttl(key);
        } catch (JedisConnectionException e) {
            borrowOrOprSuccess = false;
            if (jedis != null) jedisPool.returnBrokenResource(jedis);
        } finally {
            if (borrowOrOprSuccess) jedisPool.returnResource(jedis);
        }
        return l;
    }

    /**
     * 原子自增
     * 
     * @param key
     * @return
     */
    public static Long incr(String key) {
        Jedis jedis = null;
        Long l = 0L;
        boolean borrowOrOprSuccess = true;
        try {
            jedis = jedisPool.getResource();
            l = jedis.incr(key);
        } catch (JedisConnectionException e) {
            borrowOrOprSuccess = false;
            if (jedis != null) jedisPool.returnBrokenResource(jedis);
        } finally {
            if (borrowOrOprSuccess) jedisPool.returnResource(jedis);
        }
        return l;
    }
    /**
     * 原子自减
     * 
     * @param key
     * @return
     */
    public static Long decr(String key) {
        Jedis jedis = null;
        Long l = 0L;
        boolean borrowOrOprSuccess = true;
        try {
            jedis = jedisPool.getResource();
            l = jedis.decr(key);
        } catch (JedisConnectionException e) {
            borrowOrOprSuccess = false;
            if (jedis != null) jedisPool.returnBrokenResource(jedis);
        } finally {
            if (borrowOrOprSuccess) jedisPool.returnResource(jedis);
        }
        return l;
    }

    /**
     * 移除
     * 
     * @param key
     * @param i
     * @param value
     */
    public static void remove(String key) {
        Jedis jedis = null;
        boolean borrowOrOprSuccess = true;
        try {
            jedis = jedisPool.getResource();
            jedis.del(key);
        } catch (JedisConnectionException e) {
            borrowOrOprSuccess = false;
            if (jedis != null) jedisPool.returnBrokenResource(jedis);
        } finally {
            if (borrowOrOprSuccess) jedisPool.returnResource(jedis);
        }
    }

    /**
     * 判断是否存在
     */
    public static boolean contains(String key) {
        Jedis jedis = null;
        boolean borrowOrOprSuccess = true;
        boolean f = false;
        try {
            jedis = jedisPool.getResource();
            f = jedis.exists(key);
        } catch (JedisConnectionException e) {
            borrowOrOprSuccess = false;
            if (jedis != null) jedisPool.returnBrokenResource(jedis);
        } finally {
            if (borrowOrOprSuccess) jedisPool.returnResource(jedis);
        }
        return f;
    }

    public static void set(String key, long index, String value) {
        Jedis jedis = null;
        boolean borrowOrOprSuccess = true;
        try {
            jedis = jedisPool.getResource();
            jedis.zadd(key, index, value);
        } catch (JedisConnectionException e) {
            borrowOrOprSuccess = false;
            if (jedis != null) jedisPool.returnBrokenResource(jedis);
        } finally {
            if (borrowOrOprSuccess) jedisPool.returnResource(jedis);
        }
    }

}
